<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin\DealRecord;
use Illuminate\Http\Request;

class DealController extends Controller
{
    public function getList(Request $request)
    {
        $pageSize = $request['pageSize'];
        $storeId = $request['storeId'];
        $userId = $request['userId'];
        $dealItemId = $request['dealItemId'];
        $goldType = $request['goldType'];
        $begin_at = $request['begin_at'];
        $end_at = $request['end_at'];
        $grade = $request['grade'];
        $deal_type = $request['deal_type'];
        $status = $request['status'];
        $recharge_type = $request['recharge_type'];
        $report = $request['report'];

        $dealModel = new DealRecord;
        $data = $dealModel->getList($pageSize, $storeId, $userId, $dealItemId, $goldType, $begin_at, $end_at, $grade, $deal_type, $status, $recharge_type, $report);

        return $data;
    }

    //余额审核
    public function reviewAmount(Request $request){
        $dealModel = new DealRecord;
        return $dealModel->reviewAmount($request);
    }

    // [余额]扣款
    public function deductMoney($goldRecord){
        $goldRecord['type'] = 'out';
        $record = $this->changeInfo($goldRecord);
        if ($record['code'] != 20000 ) {
            return $record;
        }
        
        DB::beginTransaction();
        try {
            $insert = $this->insert($record['data']);
            if ($insert && $record['data']['status'] == 1) {
                $keyYu = $this->getYu($record['data']['jm_type']);
                $decrement = User::where('id', $record['data']['user_id'])->decrement($keyYu, $record['data']['amount']);//余额生效 出
                if ($decrement) {
                    DB::commit();
                    return $this->successClient($record, '[扣除]已生效交易记录生成成功!'); 
                } else {
                    return $this->errorClient($record, '[扣除]已生效交易记录生成失败!');
                }
            } else {
                DB::commit();
                return $this->successClient($record, '[扣除]未生效交易记录生成成功!'); 
            }
        } catch (\Exception $e) {
            DB::rollBack();
            return $e->getLine().$e->getMessage();
        }
    }
    // [余额]回款
    public function returnedMoney($goldRecord){
        $goldRecord['type'] = 'in';
        $record = $this->changeInfo($goldRecord);
        if ($record['code'] != 20000 ) {
            return $this->errorClient($goldRecord, '扣款数据不合法');
        }
        DB::beginTransaction();
        try {
            $insert = $this->insert($record['data']);
            if ($insert && $record['data']['status'] == 3) {
                $keyYu = $this->getYu($record['data']['jm_type']);
                $increment = User::where('id',$record['data']['user_id'])->increment($keyYu, $record['data']['amount']);//余额生效 出
                if ($increment) {
                    DB::commit();
                    return $this->successClient($record, '[回款]已生效交易记录生成成功!'); 
                } else {
                    return $this->errorClient($record, '[回款]已生效交易记录生成失败!'.$increment);
                }
            } else {
                DB::commit();
                return $this->successClient($record, '[回款]未生效交易记录生成成功!'); 
            }
        } catch (\Exception $e) {
            DB::rollBack();
            return $e->getLine().$e->getMessage();
        }
    }

    // 金额 / 积分 / 军功章 统一逻辑
    private function changeInfo($goldRecord)
    {
        $record=[];
        $record['type'] = $goldRecord['type'];
        $record['store_id'] = $goldRecord['store_id'];
        $record['user_id'] = $goldRecord['user_id'];
        $record['status'] = $goldRecord['status'];
        $record['reason'] = $goldRecord['reason'];
        $record['deal_type'] = $goldRecord['deal_type'];
        $record['deal_item_id'] = $goldRecord['deal_item_id'];
        $record['order_type'] = $goldRecord['order_type'];
        $record['jm_type'] = $goldRecord['jm_type'];
        $record['grade'] = $goldRecord['grade'];
        $record['mark'] = isset($goldRecord['mark']) ? $goldRecord['mark'] : '';
        $record['amount'] = $goldRecord['amount'];
        $record['created_at'] = date('Y-m-d H:i:s');
        
        if (!isset($goldRecord['last_balance'])) {
            $userInfo = User::where('id', $goldRecord['user_id'])->select('gold', 'jf', 'jgz')->first();
            $keyYu = $this->getYu($goldRecord['jm_type']);
            $record['last_balance'] = $userInfo[$keyYu];
        } else {
            $record['last_balance'] = $goldRecord['last_balance'];
        }
        $record['new_balance'] = $record['type'] == 'in' ? $record['last_balance'] + $record['amount'] : $record['last_balance'] - $record['amount'];
        if ($record['new_balance'] < 0) {
            return $this->errorClient($record, '[失败:'.$goldRecord['jm_type'].']交易后余额为负!');
        } else {
            return $this->successClient($record, '交易记录数据准备成功!'); 
        }
    }
    public function getYu($jm_type)
    {
        $str = '';
        switch ($jm_type) {
            case 1:
                $str = 'gold';
                break;
            case 2:
                $str = 'jf';
                break;
            case 3:
                $str = 'jgz';
                break;
            case 4:
                $str = 'bd_index';
                break;               
            default:
                # code...
                break;
        }
        return $str;
    }
}
